/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor. 
 */
package easybooking.clients.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author tim
 */
public class CustomerCreateDialog extends JDialog {

    EBInterface ebi;
    private JLabel lblFirstName;
    private JLabel lblLastName;
    private JLabel lblStreet;
    private JLabel lblZip;
    private JLabel lblCity;
    private JTextField txfFirstName;
    private JTextField txfLastName;
    private JTextField txfStreet;
    private JTextField txfZip;
    private JTextField txfCity;
    private JPanel dialogPanel;
    private JPanel buttonPanel;
    private JButton btnOk;
    private JButton btnCancel;

    public CustomerCreateDialog(EBInterface ebif) {
        ebi = ebif;
        //generateComponents();
    }
    // Get Text Fields

    public String getFirstName() {
        return txfFirstName.getText();
    }

    public String getLastName() {
        return txfLastName.getText();
    }

    public String getStreet() {
        return txfStreet.getText();
    }

    public String getZip() {
        return txfZip.getText();
    }

    public String getCity() {
        return txfCity.getText();
    }

   

    //shows the dialog when needed
    public void createAndShowDialog() {
        generateComponents();
        this.setResizable(true);
        this.setTitle("Create Customer");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }
 
    // Preperes the dialog
    private void generateComponents() {

        // Dialog Settings

        dialogPanel = new JPanel();


        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }



        dialogPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        lblFirstName = new JLabel("First Name:");
        addComponent(dialogPanel, gbl, lblFirstName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);

        txfFirstName = new JTextField(10);
        addComponent(dialogPanel, gbl, txfFirstName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

        lblLastName = new JLabel("Last Name:");
        addComponent(dialogPanel, gbl, lblLastName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 1, 1, 1, 0, 0);

        txfLastName = new JTextField(10);
        addComponent(dialogPanel, gbl, txfLastName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 1, 1, 1, 0, 0);

        lblStreet = new JLabel("Street:");
        addComponent(dialogPanel, gbl, lblStreet, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 1, 1, 0, 0);

        txfStreet = new JTextField(10);
        addComponent(dialogPanel, gbl, txfStreet, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 2, 1, 1, 0, 0);

        lblZip = new JLabel("Zip-Code:");
        addComponent(dialogPanel, gbl, lblZip, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 3, 1, 1, 0, 0);

        txfZip = new JTextField(10);
        addComponent(dialogPanel, gbl, txfZip, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 3, 1, 1, 0, 0);

        lblCity = new JLabel("City:");
        addComponent(dialogPanel, gbl, lblCity, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 4, 1, 1, 0, 0);

        txfCity = new JTextField(10);
        addComponent(dialogPanel, gbl, txfCity, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 4, 1, 1, 0, 0);

        // adds to buttonPanel
        addComponent(dialogPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 6, 2, 1, 0, 0);

        // sets die dialogPanel as contentPane
        this.setContentPane(dialogPanel);


    }

    // Creates the button panel
    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();

        btnOk = new JButton("OK");
        btnCancel = new JButton("Cancel");

        // Sets the buttons the same size
        btnOk.setPreferredSize(btnCancel.getPreferredSize());

        // Registrate action listeners
        btnOk.setActionCommand("customerCreateDialogOk");
        btnOk.addActionListener(ebi);

        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // Adds the buttons to the buttonPanel
        buttonPanel.add(btnOk);
        buttonPanel.add(btnCancel);
        return buttonPanel;
    }

    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
